/**
 * Token临期换新
 */
(function () {
  const expires = 3600;   // Token1小时过期

  return function ({ UID, IP }, headers) {
    const real_ip = (headers['X-Real-IP'] || []).join(',');
    const keepIp = A.setting('TokenRenew.KeepIP'); // 内网环境可以开启IP匹配

    if (UID) {
      if (keepIp && (IP !== real_ip)) throw Error('登录失败: IP地址不匹配');
      const token = A.jwt.create({ UID, IP }, expires);
      return { token, expires };
    }

    throw Error('登录失败');
  };
})();